package com.fingerth.crawler.room;

import android.text.TextUtils;
import android.view.TextureView;

import androidx.room.Entity;
import androidx.room.PrimaryKey;

import com.fingerth.crawler.bean.NovelChapterListBean;

import java.util.ArrayList;

@Entity
public class NovelChapter {
    @PrimaryKey(autoGenerate = true)
    public Long id;

    public String fictionId;
    public String chapterId;
    public String title;
    public String creationTime;
    public Long cid;

    public ArrayList<String> content;

//    @Ignore
//    private String contentTxt;
//
//    @Ignore
//    public String getContentTxt(Context context) {
//        if (TextUtils.isEmpty(contentTxt)) {
//            StringBuilder builder = new StringBuilder();
//            if (content != null && content.size() > 0) {
//                for (int i = 0; i < content.size(); i++) {
//                    builder.append(context.getString(R.string.space)).append(context.getString(R.string.space))
//                            .append(context.getString(R.string.space))//.append(context.getString(R.string.space))
//                            .append(content.get(i));//.append("\r\n");
//                }
//            }
//            contentTxt = builder.toString();
//        }
//        return contentTxt;
//    }


    public NovelChapter() {

    }

    public NovelChapter(NovelChapterListBean.NovelChapterItemBean chapter) {
        this.creationTime = chapter.creationTime;
        if (TextUtils.isEmpty(chapter.chapterId)) {
            this.chapterId = "0";
        } else {
            this.chapterId = chapter.chapterId;
        }
        this.fictionId = chapter.fictionId;
        this.title = chapter.title;
        this.cid = chapter.id;
    }


    public boolean change(NovelChapterListBean.NovelChapterItemBean bean) {
        boolean isChange = false;
        if (!TextUtils.equals(this.fictionId, bean.fictionId)) {
            this.fictionId = bean.fictionId;
        }
        if (!TextUtils.equals(this.title, bean.title)) {
            this.title = bean.title;
            isChange = true;
        }
        if (!TextUtils.equals(this.chapterId, bean.chapterId)) {
            if (TextUtils.isEmpty(bean.chapterId)) {
                this.chapterId = "0";
            } else {
                this.chapterId = bean.chapterId;
            }
            isChange = true;
        }

        if (!TextUtils.equals(this.creationTime, bean.creationTime)) {
            this.creationTime = bean.creationTime;
            isChange = true;
        }
        if (!this.cid.equals(bean.id)) {
            this.cid = bean.id;
            isChange = true;
        }
        return isChange;
    }

    public boolean change(ArrayList<String> content) {
        boolean isChange = false;
        if (this.content != null) {
            if (content != null) {
                if (this.content.size() == content.size()) {
                    for (int i = 0; i < this.content.size(); i++) {
                        if (!TextUtils.equals(this.content.get(i), content.get(i))) {
                            isChange = true;
                            break;
                        }
                    }
                } else {
                    isChange = true;
                }
            }
        } else if (content != null) {
            isChange = true;
        }
        if (isChange) {
            if (this.content == null) {
                this.content = content;
            } else {
                this.content.clear();
                this.content.addAll(content);
            }
        }

        return isChange;
    }


}
